#!/usr/bin/env python
# encoding: utf-8
# @author: Lin Han
# @contact: voldemort22@126.com
# @file: add_glb_to_standard_part.py
# @time: 2021/6/1 11:03
# @desc:
from .utils.md5_tools import getFileMd5
from .utils.mongo_tools import retrieve_all_mongo_values, update_mongo_value
from .utils.oss.oss import download_file_from_oss, upload_file_to_oss
from .views.configs import STANDARD_PART_COLLECTION, JSON_OSS_ADDRESS, GLTF_OSS_ADDRESS, FACTORY_STANDARD_COLLECTION, CATPART_OSS_ADDRESS

standard_parts = retrieve_all_mongo_values({}, collection_name=STANDARD_PART_COLLECTION)

volvo = retrieve_all_mongo_values({}, collection_name=FACTORY_STANDARD_COLLECTION)[0]
standard_part_mapping = volvo['part_name_rule']


for i in standard_parts:
    json_address = i["file"][JSON_OSS_ADDRESS]
    # glb_address = json_address.replace("json5", "glb")
    # download_file_from_oss(glb_address)
    # glb_md5 = getFileMd5(glb_address)
    # i["file"][GLTF_OSS_ADDRESS] = glb_address
    # i["file"]["gltfMD5"] = glb_md5
    # catpart_local_address = standard_part_mapping[i['name'].replace(".json5", "")]['path']
    catpart_oss_address = json_address.replace("json5", "CATPart")
    # upload_file_to_oss(local_file_name=catpart_local_address, oss_file_dirname=catpart_oss_address)
    try:
        download_file_from_oss(catpart_oss_address)
        catpart_md5 = getFileMd5(catpart_oss_address)
        i["file"][CATPART_OSS_ADDRESS] = catpart_oss_address
        i["file"]["catpartMD5"] = catpart_md5
    except:
        pass

    print(i)
    # update_mongo_value(filter_condition={"number": i["number"]}, new_value={"$set": i}, collection_name=STANDARD_PART_COLLECTION)
